<?php


namespace addon\hotel\hotel\controller;

use app\Controller;
use app\model\hotel\Hotel;
use app\model\system\User as UserModel;
use app\model\web\Config as ConfigModel;
use app\model\web\WebSite as WebsiteModel;
use think\captcha\facade\Captcha as ThinkCaptcha;
use think\facade\Cache;

/**
 * 登录
 */
class Login extends Controller
{

    protected $replace = [];    //视图输出字符串内容替换    相当于配置文件中的'view_replace_str'
    protected $app_module = "hotel";

    public function __construct()
    {
        //执行父类构造函数
        parent::__construct();
        $this->replace = [
            'HOTEL_CSS' => __ROOT__ . '/addon/hotel/hotel/view/public/css',
            'HOTEL_JS' => __ROOT__ . '/addon/hotel/hotel/view/public/js',
            'HOTEL_IMG' => __ROOT__ . '/addon/hotel/hotel/view/public/img',
        ];
    }

    /**
     * 登录首页
     * @return mixed
     */
    public function login()
    {
        $config_model = new ConfigModel();
        $website_model = new WebsiteModel();
        $hotel_model = new Hotel();
        $config_info = $config_model->getCaptchaConfig();
        $config = $config_info[ 'data' ][ 'value' ];
        $city_login = $config[ "city_login" ] ?? 0;
        if (request()->isAjax()) {
            $username = input("username", '');
            $password = input("password", '');
            $user_model = new UserModel();
            /*if ($city_login == 1) {
                $captcha_result = $this->checkCaptcha();
                //验证码
                if ($captcha_result[ "code" ] != 0) {
                    return $captcha_result;
                }
            }*/
            $result = $user_model->login($username, $password, $this->app_module);

            if ($result[ 'code' ] == 0) {
                //查询站点状态
                $site_id = $user_model->userInfo($this->app_module);

                $website_info = $hotel_model->getHotelInfo([ [ 'hotel_id', '=', $site_id[ 'site_id' ] ] ], 'status');
                if ($website_info[ 'data' ][ 'status' ] == -1) {
                    //清除登录信息session
                    $user_model->clearLogin($this->app_module);
                    return error('-1', '酒店已关闭，请联系平台管理员');
                }
            }
            return $result;
        } else {
            $this->assign('hotel_login', $city_login);
            $this->assign("menu_info", [ 'title' => "登录" ]);
            $this->assign("hotel_info", [ 'site_name' => "酒店端" ]);

            //平台配置信息
            $website_info = $website_model->getWebSite([ [ 'site_id', '=', 0 ] ], 'web_phone,web_email,web_qrcode,web_qq,web_weixin,logo');
            $this->assign('website_info', $website_info[ 'data' ]);
            $this->assign('domain', $_SERVER[ 'SERVER_NAME' ]);

            $captcha = $this->captcha();
            $captcha = $captcha[ 'data' ];
            $this->assign("captcha", $captcha);

            //加载版权信息
            $copyright = $config_model->getCopyright();
            $this->assign('copyright', $copyright[ 'data' ][ 'value' ]);
            return $this->fetch("login/login", [], $this->replace);
        }

    }

    /**
     * 退出操作
     */
    public function logout()
    {
        $user_model = new UserModel();
        $uid = $user_model->uid($this->app_module);
        if ($uid > 0) {
            //清除登录信息session
            $user_model->clearLogin($this->app_module);
            $this->redirect(addon_url("hotel://hotel/login/login"));
        } else {
            $this->redirect(addon_url("hotel://hotel/login/login"));
        }
    }

    /**
     * 验证码
     */
    public function captcha()
    {
        $captcha_data = ThinkCaptcha::create(null, true);
        $captcha_id = md5(uniqid(null, true));
        // 验证码10分钟有效
        Cache::set($captcha_id, $captcha_data[ 'code' ], 600);
        return success(0, '', [ 'id' => $captcha_id, 'img' => $captcha_data[ 'img' ] ]);
    }

    /**
     * 验证码验证
     */
    public function checkCaptcha()
    {
        $captcha = input('captcha', '');

        if (empty($captcha)) return error(-1, '请输入验证码');
        if (!captcha_check($captcha)) return error(-1, '验证码错误');

        return success();
    }

    /**
     * 修改密码
     * */
    public function modifyPassword()
    {
        if (request()->isAjax()) {
            $user_model = new UserModel();
            $uid = $user_model->uid($this->app_module);

            $old_pass = input('old_pass', '');
            $new_pass = input('new_pass', '123456');

            $res = $user_model->modifyAdminUserPassword($uid, $old_pass, $new_pass);

            return $res;
        }
    }

    /**
     * 清理缓存
     */
    public function clearCache()
    {
        Cache::clear();
        return success('', '缓存更新成功', '');
    }

}